import rdflib

from rdflib.Graph import Graph
from rdflib import Namespace
from rdflib import Literal
from rdflib import URIRef
from rdflib.store import Store, NO_STORE, VALID_STORE
from rdflib import plugin


store = plugin.get('SQLite', Store)('rdfstore')

default_graph_uri = "http://rdflib.net/rdfstore2"
configString = "./"

rt = store.open(configString,create=False)
if rt != VALID_STORE:
    # There is no underlying MySQL infrastructure, create it
    store.open(configString,create=True)
else:
    assert rt == VALID_STORE,"There underlying store is corrupted"

# Declare namespaces to use.
ns_sn = Namespace("http://www.snee.com/ns/misc#")
ns_sd = Namespace("http://www.snee.com/docs/")
ns_dc = Namespace("http://purl.org/dc/elements/1.1/")
ns_pr = Namespace("http://prismstandard.org/1.0#")

# There is a store, use it
graph = Graph(store, identifier = URIRef(default_graph_uri))


# Add triples to store.
graph.add((ns_sd["d1001"], ns_dc["title"], Literal("Sample Acrobat document")))
graph.add((ns_sd["d1001"], ns_dc["format"], Literal("PDF")))
graph.add((ns_sd["d1001"], ns_dc["creator"], Literal("Billy Shears")))
graph.add((ns_sd["d1001"], ns_pr["publicationTime"], Literal("2002-12-19")))

graph.add((ns_sd["d1002"], ns_dc["title"], Literal("Sample RTF document")))
graph.add((ns_sd["d1002"], ns_dc["format"], Literal("RTF")))
graph.add((ns_sd["d1002"], ns_dc["creator"], Literal("Nanker Phelge")))
graph.add((ns_sd["d1002"], ns_pr["publicationTime"], Literal("2002-12-15")))

graph.add((ns_sd["d1003"], ns_dc["title"], Literal("Sample LaTeX document")))
graph.add((ns_sd["d1003"], ns_dc["format"], Literal("LaTeX")))
graph.add((ns_sd["d1003"], ns_dc["creator"], Literal("Richard Mutt")))
graph.add((ns_sd["d1003"], ns_pr["publicationTime"], Literal("2002-12-16")))
graph.add((ns_sd["d1003"], ns_sn["quality"], Literal("pretty good")))

graph.add((ns_sd["d1001"], ns_dc["issued_by"], ns_pr['obj1']))

graph.commit()

# display the graph in RDF/XML
#print graph.serialize(format='nt')

# Output information about one document.
docID = "d1003"
print "Information about document " + docID + ":"
for docInfo in graph.predicate_objects(ns_sd[docID]):
    print docInfo
